﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DataModel;

namespace BasicDataManagement
{
    /// <summary>
    /// Interaction logic for GoodsMngWindow.xaml
    /// </summary>
    public partial class DataMngWindow : Window
    {
        IOpertation op;

        ObservableCollection<string> materialCat;
        ObservableCollection<string> comboCat;
        ObservableCollection<string> fixedDishCat;
        ObservableCollection<string> replaceableCat;
        ObservableCollection<string> optionCat;

        ContextMenu cm;

        public DataMngWindow()
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;

            op = OperationFactory.Create();
            LoadTreeCat();
            cm = listViewThing.ContextMenu;
            listViewThing.ContextMenu = null;
        }

        private void LoadTreeCat()
        {
            materialCat = new ObservableCollection<string>(op.LoadMaterialCategory());
            LoadCat(materialTVI, materialCat);

            comboCat = new ObservableCollection<string>(op.LoadComboCategory());
            LoadCat(comboTVI, comboCat);

            fixedDishCat = new ObservableCollection<string>(op.LoadFixedDishCategory());
            LoadCat(fixedDishTVI, fixedDishCat);

            optionCat = new ObservableCollection<string>(op.LoadOptionCategory());
            LoadCat(optionTVI, optionCat);

            replaceableCat = new ObservableCollection<string>(op.LoadReplaceableDishCategory());
            LoadCat(replaceableDishTVI, replaceableCat);
        }

        private void LoadCat(TreeViewItem tvi, ObservableCollection<string> cat)
        {
            tvi.Items.Clear();
            //tvi.ItemsSource = cat;
            foreach (var t in cat)
            {
                //System.Diagnostics.Debug.WriteLine(t.GetType().ToString());
                //(t as TreeViewItem).ContextMenu = this.FindResource("SubcatCM") as ContextMenu;
                TreeViewItem tmp = new TreeViewItem();
                tmp.Header = t;
                tmp.ContextMenu = this.FindResource("SubcatCM") as ContextMenu;
                tmp.Selected += new RoutedEventHandler(treeViewItemSubcat_Selected);

                tvi.Items.Add(tmp);
            }
        }

        private void TreeViewItem_Selected(object sender, RoutedEventArgs e)
        {
            (e.Source as TreeViewItem).IsSelected = true;
        }

        private void TreeViewItem_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            (e.Source as TreeViewItem).IsSelected = true;
            //System.Diagnostics.Debug.WriteLine((e.Source as TreeViewItem).IsSelected = true);
            System.Diagnostics.Debug.WriteLine((sender as TreeViewItem).Header + " : " + (e.Source as TreeViewItem).Header);
        }

        private void AddCatMI_Click(object sender, RoutedEventArgs e)
        {
            ObjectType objType;
            switch ((treeViewAll.SelectedItem as TreeViewItem).Header.ToString())
            {
                case "单品":
                    {
                        objType = ObjectType.FixedDish;
                        break;
                    }
                case "套餐":
                    {
                        objType = ObjectType.Combo;
                        break;
                    }
                case "配菜":
                    {
                        objType = ObjectType.Option;
                        break;
                    }
                case "自选菜":
                    {
                        objType = ObjectType.ReplaceableDish;
                        break;
                    }
                case "原料":
                    {
                        objType = ObjectType.Material;
                        break;
                    }
                default:
                    {
                        return;
                    }
            }

            TreeViewItem source = (ContextMenuService.GetPlacementTarget(LogicalTreeHelper.GetParent(sender as MenuItem))) as TreeViewItem;

            AddNewCatWindow newCat = new AddNewCatWindow();
            newCat.Owner = this;
            newCat.WindowStartupLocation = WindowStartupLocation.CenterOwner;

            if (newCat.ShowDialog() == true)
            {
                op.CreateCategory(objType, newCat.CatName);
                TreeViewItem tvi = new TreeViewItem();
                tvi.Header = newCat.CatName;
                tvi.ContextMenu = this.FindResource("SubcatCM") as ContextMenu;
                tvi.Selected += new RoutedEventHandler(treeViewItemSubcat_Selected);
                source.Items.Add(tvi);
                //System.Diagnostics.Debug.WriteLine((((sender as MenuItem).Parent as ContextMenu).Parent).GetType().ToString());
                //((ContextMenuService.GetPlacementTarget(LogicalTreeHelper.GetParent(sender as MenuItem))) as TreeViewItem).Items.Add(tvi);
                //(sender as TreeViewItem).Items.Add(tvi);
            }
        }

        private void AddItem_Click(object sender, RoutedEventArgs e)
        {
            string catType = ((treeViewAll.SelectedItem as TreeViewItem).Parent as TreeViewItem).Header.ToString();
            string defaultCatName = (treeViewAll.SelectedItem as TreeViewItem).Header.ToString();
            Window addWin = AddItemFactory.Create(catType, op, defaultCatName);
                //(addWin as AddItemBaseWindow).Cats = op.LoadMaterialCategory();
            addWin.Owner = this;
            addWin.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            //MessageBox.Show(treeViewAll.SelectedItem.ToString());
            if (addWin.ShowDialog() == true)
            {
                switch (catType)
                {
                    case "套餐":
                        {
                            op.CreateObject(ObjectType.Combo, (addWin as AddComboItemWindow).comboObject);
                            listViewThing.ItemsSource = null;
                            listViewThing.ItemsSource = new ObservableCollection<ComboObject>(op.LoadComboObjects((treeViewAll.SelectedItem as TreeViewItem).Header.ToString()));
                            //TODO: 刷新列表
                            break;
                        }
                    case "单品":
                        {
                            op.CreateObject(ObjectType.FixedDish, (addWin as AddFixedDishItemWindow).fixedDishObject);
                            listViewThing.ItemsSource = null;
                            listViewThing.ItemsSource = new ObservableCollection<FixedDishObject>(op.LoadFixedDishObjects((treeViewAll.SelectedItem as TreeViewItem).Header.ToString()));
                            //TODO: 刷新列表
                            break;
                        }
                    case "配菜":
                        {
                            op.CreateObject(ObjectType.Option, (addWin as AddOptionItemWindow).optionObject);
                            listViewThing.ItemsSource = null;
                            listViewThing.ItemsSource = new ObservableCollection<OptionObject>(op.LoadOptionObjects((treeViewAll.SelectedItem as TreeViewItem).Header.ToString()));
                            //(listViewThing.ItemsSource as ObservableCollection<OptionObject>).Add((addWin as AddOptionItemWindow).optionObject);
                            //TODO: 刷新列表
                            break;
                        }
                    case "原料":
                        {
                            op.CreateObject(ObjectType.Material, (addWin as AddMaterialItemWindow).materialObject);
                            listViewThing.ItemsSource = null;
                            listViewThing.ItemsSource = new ObservableCollection<MaterialObject>(op.LoadMaterialObjects((treeViewAll.SelectedItem as TreeViewItem).Header.ToString()));
                            //(listViewThing.ItemsSource as ObservableCollection<MaterialObject>).Add((addWin as AddMaterialItemWindow).materialObject);
                            //TODO: 刷新列表
                            break;
                        }
                    case "自选菜":
                        {
                            op.CreateObject(ObjectType.ReplaceableDish, (addWin as AddReplaceableDishItemWindow).rDishObject);
                            listViewThing.ItemsSource = null;
                            listViewThing.ItemsSource = new ObservableCollection<ReplaceableDishObject>(op.LoadReplaceableDishObjects((treeViewAll.SelectedItem as TreeViewItem).Header.ToString()));
                            //(listViewThing.ItemsSource as ObservableCollection<ReplaceableDishObject>).Add((addWin as AddReplaceableDishItemWindow).rDishObject);
                            //TODO: 刷新列表
                            break;
                        }
                    default: break;
                }
            }
        }

        private void AddCatFromSubMI_Click(object sender, RoutedEventArgs e)
        {
            TreeViewItem source = ((ContextMenuService.GetPlacementTarget(LogicalTreeHelper.GetParent(sender as MenuItem))) as TreeViewItem).Parent as TreeViewItem;

            AddNewCatWindow newCat = new AddNewCatWindow();
            newCat.Owner = this;
            newCat.WindowStartupLocation = WindowStartupLocation.CenterOwner;

            if (newCat.ShowDialog() == true)
            {
                TreeViewItem tvi = new TreeViewItem();
                tvi.Header = newCat.CatName;
                source.Items.Add(tvi);
                //System.Diagnostics.Debug.WriteLine((((sender as MenuItem).Parent as ContextMenu).Parent).GetType().ToString());
                //((ContextMenuService.GetPlacementTarget(LogicalTreeHelper.GetParent(sender as MenuItem))) as TreeViewItem).Items.Add(tvi);
                //(sender as TreeViewItem).Items.Add(tvi);
            }
        }

        private void RenameCatMI_Click(object sender, RoutedEventArgs e)
        {
            TreeViewItem source = (ContextMenuService.GetPlacementTarget(LogicalTreeHelper.GetParent(sender as MenuItem))) as TreeViewItem;

            AddNewCatWindow cat = new AddNewCatWindow();
            cat.Owner = this;
            cat.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            cat.newNameTB.Text = source.Header.ToString();
            string prevousname = source.Header.ToString();
            string type = (source.Parent as TreeViewItem).Header.ToString();
            if (cat.ShowDialog() == true)
            {
                source.Header = cat.CatName;
                op.UpdateSubcatName(type, prevousname, cat.CatName);
            }
        }

        //private void DelCatMI_Click(object sender, RoutedEventArgs e)
        //{
        //    TreeViewItem source = (ContextMenuService.GetPlacementTarget(LogicalTreeHelper.GetParent(sender as MenuItem))) as TreeViewItem; 
        //    if (MessageBox.Show("是否删除？", "确认", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.Yes)
        //    {
        //        (source.Parent as TreeViewItem).Items.Remove(source);
        //    }
        //}



        private void treeViewItemSubcat_Selected(object sender, RoutedEventArgs e)
        {
            TreeViewItem source = e.Source as TreeViewItem;
            string sourceParentName = (source.Parent as TreeViewItem).Header.ToString();

            switch (sourceParentName)
            {
                case "原料":
                    {
                        ObservableCollection<MaterialObject> materialObjList = new ObservableCollection<MaterialObject>(op.LoadMaterialObjects((e.Source as TreeViewItem).Header.ToString()));
                        listViewThing.ItemsSource = materialObjList;
                        listViewThing.ContextMenu = this.FindResource("ListViewOpCM") as ContextMenu;
                        break;
                    }
                case "单品":
                    {
                        ObservableCollection<FixedDishObject> fdObjList = new ObservableCollection<FixedDishObject>(op.LoadFixedDishObjects((e.Source as TreeViewItem).Header.ToString()));
                        listViewThing.ItemsSource = fdObjList;
                        listViewThing.ContextMenu = this.FindResource("ListViewOpCM") as ContextMenu;
                        break;
                    }
                case "套餐":
                    {
                        ObservableCollection<ComboObject> comboObjList = new ObservableCollection<ComboObject>(op.LoadComboObjects((e.Source as TreeViewItem).Header.ToString()));
                        listViewThing.ItemsSource = comboObjList;
                        listViewThing.ContextMenu = this.FindResource("ListViewOpCM") as ContextMenu;
                        break;
                    }
                case "配菜":
                    {
                        ObservableCollection<OptionObject> optObjList = new ObservableCollection<OptionObject>(op.LoadOptionObjects((e.Source as TreeViewItem).Header.ToString()));
                        listViewThing.ItemsSource = optObjList;
                        listViewThing.ContextMenu = this.FindResource("ListViewOpCM") as ContextMenu;
                        break;
                    }
                case "自选菜":
                    {
                        ObservableCollection<ReplaceableDishObject> rDishObjList = new ObservableCollection<ReplaceableDishObject>(op.LoadReplaceableDishObjects((e.Source as TreeViewItem).Header.ToString()));
                        listViewThing.ItemsSource = rDishObjList;
                        listViewThing.ContextMenu = this.FindResource("ListViewOpCM") as ContextMenu;
                        break;
                    }

                default:
                    {
                        listViewThing.ItemsSource = null;
                        break;
                    };
            }
            
        }

        private void treeVIMaterial_Selected(object sender, RoutedEventArgs e)
        {
            if (sender.Equals(e.Source))
            {
                ObservableCollection<MaterialObject> materialObjList = new ObservableCollection<MaterialObject>(op.LoadMaterialObjects());
                listViewThing.ItemsSource = materialObjList;
            }
        }

        private void treeVIFixedDish_Selected(object sender, RoutedEventArgs e)
        {
            if (sender.Equals(e.Source))
            {
                ObservableCollection<FixedDishObject> fdObjList = new ObservableCollection<FixedDishObject>(op.LoadFixedDishObjects());
                listViewThing.ItemsSource = fdObjList;
            }
        }

        private void treeVICombo_Selected(object sender, RoutedEventArgs e)
        {
            if (sender.Equals(e.Source))
            {
                ObservableCollection<ComboObject> comboObjList = new ObservableCollection<ComboObject>(op.LoadComboObjects());
                listViewThing.ItemsSource = comboObjList;
            }
        }

        private void treeVIOption_Selected(object sender, RoutedEventArgs e)
        {
            if (sender.Equals(e.Source))
            {
                ObservableCollection<OptionObject> optObjList = new ObservableCollection<OptionObject>(op.LoadOptionObjects());
                listViewThing.ItemsSource = optObjList;
            }
        }

        private void treeVIReplaceableDish_Selected(object sender, RoutedEventArgs e)
        {
            if (sender.Equals(e.Source))
            {
                ObservableCollection<ReplaceableDishObject> rDishObjList = new ObservableCollection<ReplaceableDishObject>(op.LoadReplaceableDishObjects());
                listViewThing.ItemsSource = rDishObjList;
            }
        }

        private void listViewThing_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (listViewThing.SelectedItem == null)
            {
                return;
            }
            if (treeViewAll.SelectedItem == null)
            {
                return;
            }
            else
            {
                string selectedHead = (treeViewAll.SelectedItem as TreeViewItem).Header.ToString();
                if (selectedHead.Equals("所有物品") && selectedHead.Equals("货品") && selectedHead.Equals("产品")
                    && selectedHead.Equals("原料") && selectedHead.Equals("其他物品")
                    && selectedHead.Equals("套餐") && selectedHead.Equals("单品") && selectedHead.Equals("自选菜") && selectedHead.Equals("配菜"))
                {
                    return;
                }
            }
            //MessageBox.Show((e.Source as ListView).SelectedItem.GetType().ToString());
            object selectedItem = (e.Source as ListView).SelectedItem;
            string PreviousName = null;
            if (selectedItem.GetType() == typeof(MaterialObject))
            {
                AddMaterialItemWindow win = new AddMaterialItemWindow();
                win.Owner = this;
                win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                win.materialObject = selectedItem as MaterialObject;
                PreviousName = win.materialObject.ObjectName;
                win.state = EditState.Modify;
                win.Cats = new ObservableCollection<string>(op.LoadMaterialCategory());
                win.Title = "编辑原料";
                if (win.ShowDialog() == true)
                {
                    op.UpdateMaterial(PreviousName, win.materialObject);
                    int selectedIndex = (e.Source as ListView).SelectedIndex;
                    ObservableCollection<MaterialObject> tmp = new ObservableCollection<MaterialObject>(((e.Source as ListView).ItemsSource as ObservableCollection<MaterialObject>).ToList());
                    (e.Source as ListView).SelectedIndex = selectedIndex;
                    (e.Source as ListView).ItemsSource = tmp;
                }
                //TODO: 刷新列表
                return;
            }
            if (selectedItem.GetType() == typeof(OptionObject))
            {
                AddOptionItemWindow win = new AddOptionItemWindow();
                win.Owner = this;
                win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                win.optionObject = selectedItem as OptionObject;
                PreviousName = win.optionObject.ObjectName;
                win.Cats = new ObservableCollection<string>(op.LoadOptionCategory());
                win.Title = "编辑配菜";
                if (win.ShowDialog() == true)
                {
                    op.UpdateOption(PreviousName, win.optionObject);
                    int selectedIndex = (e.Source as ListView).SelectedIndex;
                    ObservableCollection<OptionObject> tmp = new ObservableCollection<OptionObject>(((e.Source as ListView).ItemsSource as ObservableCollection<OptionObject>).ToList());
                    (e.Source as ListView).SelectedIndex = selectedIndex;
                    (e.Source as ListView).ItemsSource = tmp;
                }
                //TODO: 刷新列表
                return;
            }
            if (selectedItem.GetType() == typeof(ComboObject))
            {
                AddComboItemWindow win = new AddComboItemWindow();
                win.Owner = this;
                win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                win.comboObject = selectedItem as ComboObject;
                PreviousName = win.comboObject.ObjectName;
                win.Cats = new ObservableCollection<string>(op.LoadComboCategory());
                win.Title = "编辑套餐";
                if (win.ShowDialog() == true)
                {
                    op.UpdateCombo(PreviousName, win.comboObject);
                    int selectedIndex = (e.Source as ListView).SelectedIndex;
                    ObservableCollection<ComboObject> tmp = new ObservableCollection<ComboObject>(((e.Source as ListView).ItemsSource as ObservableCollection<ComboObject>).ToList());
                    (e.Source as ListView).ItemsSource = tmp;
                    (e.Source as ListView).SelectedIndex = selectedIndex;
                }
                //TODO: 刷新列表                
                return;
            }
            if (selectedItem.GetType() == typeof(FixedDishObject))
            {
                AddFixedDishItemWindow win = new AddFixedDishItemWindow();
                win.Owner = this;
                win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                win.fixedDishObject = selectedItem as FixedDishObject;
                PreviousName = win.fixedDishObject.ObjectName;
                win.Cats = new ObservableCollection<string>(op.LoadFixedDishCategory());
                win.Title = "编辑单品";
                if (win.ShowDialog() == true)
                {
                    op.UpdateFixedDish(PreviousName, win.fixedDishObject);
                    int selectedIndex = (e.Source as ListView).SelectedIndex;
                    ObservableCollection<FixedDishObject> tmp = new ObservableCollection<FixedDishObject>(((e.Source as ListView).ItemsSource as ObservableCollection<FixedDishObject>).ToList());
                    (e.Source as ListView).ItemsSource = tmp;
                    (e.Source as ListView).SelectedIndex = selectedIndex;
                }
                //TODO: 刷新列表
                return;
            }
            if (selectedItem.GetType() == typeof(ReplaceableDishObject))
            {
                AddReplaceableDishItemWindow win = new AddReplaceableDishItemWindow();
                win.Owner = this;
                win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                win.rDishObject = selectedItem as ReplaceableDishObject;
                PreviousName = win.rDishObject.ObjectName;
                win.Cats = new ObservableCollection<string>(op.LoadReplaceableDishCategory());
                win.Title = "编辑自选菜";
                if (win.ShowDialog() == true)
                {
                    op.UpdateReplaceableDish(PreviousName, win.rDishObject);
                    int selectedIndex = (e.Source as ListView).SelectedIndex;
                    ObservableCollection<ReplaceableDishObject> tmp = new ObservableCollection<ReplaceableDishObject>(((e.Source as ListView).ItemsSource as ObservableCollection<ReplaceableDishObject>).ToList());
                    (e.Source as ListView).ItemsSource = tmp;
                    (e.Source as ListView).SelectedIndex = selectedIndex;
                }
                //TODO: 刷新列表
                return;
            }
        }

        private void listViewThing_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            //ListView source = (ContextMenuService.GetPlacementTarget(LogicalTreeHelper.GetParent(sender as MenuItem))) as ListView;
            //MessageBox.Show("Hello");
            //(sender as ListView).ContextMenu = this.FindResource("ListViewOpCM") as ContextMenu;
            if ((e.Source as ListBox).ItemsSource != null)
            {
                //MessageBox.Show(treeViewAll.SelectedItem.ToString());
            }
        }

        private void treeVIDoNothing_Selected(object sender, RoutedEventArgs e)
        {
            string sourceHeader = (e.Source as TreeViewItem).Header.ToString();
            if (sourceHeader.Equals("所有物品") || sourceHeader.Equals("货品") || sourceHeader.Equals("产品"))
            {
                listViewThing.ItemsSource = null;
            }
        }

        private void treeViewAll_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (!this.FindResource("SubcatCM").Equals(((sender as TreeView).SelectedItem as TreeViewItem).ContextMenu))
            {
                listViewThing.ContextMenu = null;
            }
        }
    }
}
